package sfeir.ergosum.tools;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import sfeir.ergosum.model.Address;
import sfeir.ergosum.model.Email;
import sfeir.ergosum.model.NetworkItem;
import sfeir.ergosum.model.Person;
import sfeir.ergosum.model.Tel;

import com.google.gdata.client.spreadsheet.FeedURLFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CustomElementCollection;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetFeed;

public class SpreadsheetParser {

    private String spreadsheetKey;
    private static FeedURLFactory feedFactory = FeedURLFactory.getDefault();

    // Sample Key for dev : pGvsuLzSP31QhHFX9gUgBbw

    public SpreadsheetParser(String spreadsheetKey) {

        this.spreadsheetKey = spreadsheetKey;

    }

    public String getSpreadsheetKey() {
        return spreadsheetKey;
    }

    public void setSpreadsheetKey(String spreadsheetKey) {
        this.spreadsheetKey = spreadsheetKey;
    }

    public Person getPerson() {

        Person result = new Person();

        List<WorksheetEntry> worksheetEntries;

        WorksheetFeed feed = new WorksheetFeed();

        SpreadsheetService service = new SpreadsheetService(
                "ErgoSum-SpreadParser" + spreadsheetKey);

        InputStream is = null;
        try {
            URL spreadsheetURL = feedFactory.getWorksheetFeedUrl(
                    spreadsheetKey, "public", "values");

            try {
                is = spreadsheetURL.openStream();
            } catch (FileNotFoundException fe) {
                return null;
            }
            feed = service.getFeed(spreadsheetURL, WorksheetFeed.class);
            is.close();
            worksheetEntries = feed.getEntries();

            for (WorksheetEntry we : worksheetEntries) {

                URL listFeed = we.getListFeedUrl();

                /**
                 * <TRES IMPORTANT>
                 */
                is = listFeed.openStream();

                /**
                 * </TRES IMPORTANT>
                 */

                ListFeed lf = service.getFeed(listFeed, ListFeed.class);

                is.close();

                for (ListEntry le : lf.getEntries()) {

                    CustomElementCollection elementColl = le
                            .getCustomElements();

                    String param = elementColl.getValue("param");
                    String val = elementColl.getValue("val");

                    if (val != null && param.equals("lastname")) {
                        result.setName(val);
                    } else if (val != null && param.equals("firstname")) {
                        result.setFirstname(val);
                    } else if (val != null && param.equals("nickname")) {
                        result.setNickname(val);
                    } else if (val != null && !val.equals("")
                            && param.equals("birthday")) {
                        result.setBirthDay(val);
                    } else if (val != null && param.equals("title")) {
                        result.setTitle(val);
                    } else if (val != null && param.equals("company")) {
                        result.setCompany(val);
                    } else if (val != null && param.equals("picture")) {
                        result.setPhoto(val);
                    } else if (val != null && param.equals("email_pro")) {
                        result.getEmails().add(
                                new Email(val, Email.PROFESSIONNAL_EMAIL));
                    } else if (val != null && param.equals("email_perso")) {
                        result.getEmails().add(
                                new Email(val, Email.PERSONAL_EMAIL));
                    } else if (val != null && param.equals("adress_pro")) {
                        result.getAddresses()
                                .add(
                                        new Address(val,
                                                Address.PROFESSIONNAL_ADDRESS));
                    } else if (val != null && param.equals("adress_perso")) {
                        result.getAddresses().add(
                                new Address(val, Address.PERSONAL_ADDRESS));
                    } else if (val != null && param.equals("tel_pro")) {
                        result.getTels().add(
                                new Tel(val, Tel.PROFESSIONNAL_LINE));
                    } else if (val != null && param.equals("tel_mobile")) {
                        result.getTels().add(new Tel(val, Tel.MOBILE_LINE));
                    } else if (val != null && param.equals("tel_perso")) {
                        result.getTels().add(new Tel(val, Tel.HOME_LANDLINE));
                    } else if (val != null && param.equalsIgnoreCase("aboutMe")) {
                        result.setAboutMe(val);
                    } else if (val != null && param.equalsIgnoreCase("website")) {
                        result.setUrl(val);
                    } else if (val != null && param.substring(0, 4).equalsIgnoreCase("net_")) {
                        result.getNetworkItems().add(new NetworkItem(param.substring(4),val));
                    } /*else if (val != null && param.equals("facebook")) {
                        result.getNetworkItems().add(
                                new NetworkItem("facebook", val));
                    } else if (val != null && param.equals("twitter")) {
                        result.getNetworkItems().add(
                                new NetworkItem("twitter", val));
                    } else if (val != null && param.equals("linkedin")) {
                        result.getNetworkItems().add(
                                new NetworkItem("linkedin", val));
                    } else if (val != null && param.equals("viadeo")) {
                        result.getNetworkItems().add(
                                new NetworkItem("viadeo", val));
                    } else if (val != null && param.equals("flickr")) {
                        result.getNetworkItems().add(
                                new NetworkItem("flickr", val));
                    } else if (val != null && param.equals("vimeo")) {
                        result.getNetworkItems().add(
                                new NetworkItem("vimeo", val));
                    } else if (val != null && param.equals("dopplr")) {
                        result.getNetworkItems().add(
                                new NetworkItem("dopplr", val));
                    } else if (val != null && param.equals("lastfm")) {
                        result.getNetworkItems().add(
                                new NetworkItem("lastfm", val));
                    } else if (val != null && param.equals("readernaut")) {
                        result.getNetworkItems().add(
                                new NetworkItem("readernaut", val));
                    }*/

                }
            }
        } catch (Exception e) {
            return null;
        }

        return result;

    }
}
